import React from "react";
import { Route, Redirect } from "react-router-dom";
import { loginAuth } from "../../common/auth";
import { pages, pageIndex } from "../../common/routerConfig";

// 包装了登录拦截的Route
const Index = ({ component: Component, ...rest }) => {
  return (
    <Route
      {...rest}
      render={({ location }) =>
        loginAuth.isLogined ? (
          <Component />
        ) : (
          <Redirect
            to={{
              pathname: pages[pageIndex.login].path,
              state: { from: location },
            }}
          />
        )
      }
    />
  );
};

export default Index;
